//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.utils
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import org.as3commoncode.utils.MathUtil;	import org.as3commoncode.utils.BitmapUtil;

	/**
	 * This is a usuful class for creating sprites.
	 * 
	 * @author Tony Birleffi
	 */
	public class SpriteUtil
	{	
		/**
		 * Use this method to create a sprite.
		 *
		 * @example
		 * <listing>
         * import org.as3commoncode.utils.SpriteUtil;
         * var sprite:Sprite = SpriteUtil.createSprite(0, 0, 'sp', { alpha:50 });
         * </listing>
         * 
		 * @param xPos			x position.
		 * @param yPos			y position.
		 * @param name			string name of the mc.
		 * @param initObj		default is null, however you can pass any additional parameters, { alpha:50 }.
		 * @return MovieClip.
		 */
		public static function createSprite(xPos:Number = 0, yPos:Number = 0, name:String = null, initObj:Object = null):Sprite
		{
			// Define the new movie clip.
			var sp:Sprite = new Sprite();
			
			// Set parameters.
			sp.x = xPos;
			sp.y = yPos;
			sp.name = (name == null) ? "sp" + MathUtil.random(0, 99999, 1) + "_sp" : name;
			
			// Pass any additional properties if need be here.
			if (initObj != null) for(var it:String in initObj) sp[ it ] = initObj[ it ];
			
			// Return.
			return sp;
		}
		
		/**
		 * Method to return a class object as a sprite.
		 * 
		 * @example
		 * <listing>
         * import org.as3commoncode.utils.SpriteUtil;
         * var sprite:Sprite = SpriteUtil.createSprite(targetClass);
         * </listing>
         * 
		 * @param target	which class.
		 * @return Sprite.
		 */
		public static function asSprite(target:Class):Sprite 
		{
			var retval:Sprite = new Sprite();
			var retbmp:Bitmap = BitmapUtil.asBitmap(target);
			retval.addChild(retbmp);
			return retval;
		}
	}
}
